package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.dto.OrdersDto;
import com.itheima.reggie.entity.*;
import com.itheima.reggie.mapper.*;
import com.itheima.reggie.service.AddressBookService;
import com.itheima.reggie.service.OrderDetailService;
import com.itheima.reggie.service.OrdersService;
import com.itheima.reggie.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/order")
public class OrdersController {

    @Autowired
    private OrdersService ordersService;

    @Autowired
    private OrderDetailService orderDetailService;

    @Autowired
    private UserService userService;

    @Autowired
    private AddressBookService addressBookService;


    //生成订单
    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders){
       log.info(orders.toString());
       ordersService.submit(orders);
        return R.success("清空购物车成功");
    }

    //分页订单
    @GetMapping("/userPage")
    public R<List<OrdersDto>> pageR(Integer page, Integer pageSize){
        Page<Orders> pageInfo = new Page<>(page,pageSize);
        LambdaQueryWrapper<Orders> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Orders::getUserId,BaseContext.getCurrentId());
        ordersService.page(pageInfo,lqw);

        List<Orders> records = pageInfo.getRecords();
        List<OrdersDto> list = new ArrayList<>();
        for (Orders orders : records) {
            OrdersDto ordersDto = new OrdersDto();
            BeanUtils.copyProperties(orders,ordersDto);

            LambdaQueryWrapper<OrderDetail> lqw1 = new LambdaQueryWrapper<>();
            lqw1.eq(OrderDetail::getOrderId,orders.getId());
            List<OrderDetail> list1 = orderDetailService.list(lqw1);
            ordersDto.setOrderDetails(list1);

            LambdaQueryWrapper<User> lqw2 = new LambdaQueryWrapper<>();
            lqw2.eq(User::getId,orders.getUserId());
            User one = userService.getOne(lqw2);
            ordersDto.setUserName(one.getName());

            list.add(ordersDto);
        }
        return R.success(list);
    }


    @GetMapping("/page")
    public R<Page> pageR(Integer page,Integer pageSize,Long id){
        Page<Orders> pageInfo = new Page<>(page,pageSize);
        LambdaQueryWrapper<Orders> lqw = new LambdaQueryWrapper<>();
        lqw.eq(id!=null,Orders::getId,id);
        lqw.orderByDesc(Orders::getOrderTime);
        ordersService.page(pageInfo,lqw);
        List<Orders> records = pageInfo.getRecords();

        for (Orders orders : records) {
            LambdaQueryWrapper<User> lqw1 = new LambdaQueryWrapper<>();
            lqw1.eq(User::getId,orders.getUserId());
            User one = userService.getOne(lqw1);
            orders.setUserName(one.getName());
        }
        return R.success(pageInfo);
    }

}
